package com.cloudknows.answer;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import com.cloudknows.data.qa.zhidao.Zhidao;
import com.cloudknows.data.qa.zhidao.ZhidaoData;
import com.sun.org.apache.bcel.internal.generic.LoadClass;
import com.yzs.nlu.cloud.model.FaqItem;
import com.yzs.nlu.cloud.service.FaqFinder;
import com.yzs.nlu.cloud.service.impl.LuceneFaqFinder;

public class QaAnswer {
    // TODO:class used to contain result answer data .
    // e.g. data retrieved by baike engine.
    public FaqFinder faqFinder = new LuceneFaqFinder(); 
    
    
    
    public void load(String fileName) {
        ZhidaoData zhidaoData = new ZhidaoData();
        zhidaoData.init(fileName);
        Zhidao zhidao = null;
        List<FaqItem> faqList = new ArrayList<FaqItem>();
        String appKey = "my-appkey";
        int id = 0;
        int INTERVAL = 50000;
        
        while (null != (zhidao = zhidaoData.next())) {
            if (null == zhidao.getQuestion() || null == zhidao.getAnswers()) {
                continue;
            } 
            FaqItem faqItem = new FaqItem();
            faqItem.setQuestion(zhidao.getQuestion());
            faqItem.setAnswer(zhidao.getAnswers().get(0));
            faqItem.setAppKey(appKey);
            faqItem.setId(id);
            faqList.add(faqItem);
            id += 1;
            if (1 == id) {
                faqFinder.init(appKey, faqList);
                faqList.clear();
            } else if (0 == id % INTERVAL) {
                faqFinder.batchAddFaqItem(faqList);
                faqList.clear();
            }
        }
        if (faqList.size() > 0) {
            faqFinder.batchAddFaqItem(faqList);
            faqList.clear();
        }
                
        
    }
    
    public static void main(String[] args) throws IOException {
        QaAnswer qaAnswer = new QaAnswer();
        String fileName = "G:/DATA/zhidao_part.txt";
//        String fileName = "data/zhidao/zhidao_sample.txt";
        qaAnswer.load(fileName);
        String [] questions = {
                "红酒对身体有什么好处？怎么样喝才健康",
                "女兵招募条件",
        };
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String line = "";
        while (null != (line = reader.readLine())) {
            String answer = qaAnswer.faqFinder.search(line);
            System.out.println(line +"\n\t\t" + answer );

        }
    }
    
    
}
